﻿using Microsoft.Practices.Prism.MefExtensions.Modularity ;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Logging;

namespace OpenLightGroup.Prism
{
    [Export(typeof(IModuleManager))]
    public class SynchronizingMefModuleManager : MefModuleManager
    {
        private Microsoft.Practices.Prism.Logging.ILoggerFacade _logger;

        [ImportingConstructor]
        public SynchronizingMefModuleManager(
            IModuleInitializer moduleInitializer, 
            IModuleCatalog moduleCatalog, 
            ILoggerFacade loggerFacade )
        : base( moduleInitializer, moduleCatalog, loggerFacade )
        {
            this._logger = loggerFacade ;

            var syncCatalog = moduleCatalog as ISynchronizingModuleCatalog ;

            if (syncCatalog != null)
            {
                this._logger.Log("Using ISynchronizingModuleCatalog", Category.Debug, Priority.Low);

                syncCatalog.CatalogSynchronized += new System.EventHandler(OnCatalogSynchronized);      
            }
        }

        void OnCatalogSynchronized(object sender, System.EventArgs e)
        {
            this._logger.Log("WebModuleCatalog synced, running manager.", Category.Debug, Priority.Low);
            base.Run( ) ;
        }

    }
}